{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "!apt-get install swi-prolog -y\n",
        "!pip install pyswip langchain-google-genai langgraph langchain-core"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "iNiN42jJsbaY",
        "outputId": "b28e83cf-889a-4bdb-eb2c-ef19502e6d13"
      },
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Reading package lists... Done\n",
            "Building dependency tree... Done\n",
            "Reading state information... Done\n",
            "The following additional packages will be installed:\n",
            "  autopoint debhelper debugedit dh-autoreconf dh-strip-nondeterminism dwz\n",
            "  gettext gettext-base intltool-debian libarchive-cpio-perl\n",
            "  libarchive-zip-perl libdebhelper-perl libfile-stripnondeterminism-perl\n",
            "  libmail-sendmail-perl libossp-uuid16 libsub-override-perl\n",
            "  libsys-hostname-long-perl libtool po-debconf swi-prolog-core\n",
            "  swi-prolog-core-packages swi-prolog-doc swi-prolog-nox swi-prolog-x\n",
            "Suggested packages:\n",
            "  dh-make gettext-doc libasprintf-dev libgettextpo-dev uuid libtool-doc\n",
            "  gcj-jdk libmail-box-perl elpa-ediprolog swi-prolog-java swi-prolog-odbc\n",
            "  swi-prolog-bdb\n",
            "The following NEW packages will be installed:\n",
            "  autopoint debhelper debugedit dh-autoreconf dh-strip-nondeterminism dwz\n",
            "  gettext gettext-base intltool-debian libarchive-cpio-perl\n",
            "  libarchive-zip-perl libdebhelper-perl libfile-stripnondeterminism-perl\n",
            "  libmail-sendmail-perl libossp-uuid16 libsub-override-perl\n",
            "  libsys-hostname-long-perl libtool po-debconf swi-prolog swi-prolog-core\n",
            "  swi-prolog-core-packages swi-prolog-doc swi-prolog-nox swi-prolog-x\n",
            "0 upgraded, 25 newly installed, 0 to remove and 35 not upgraded.\n",
            "Need to get 9,926 kB of archives.\n",
            "After this operation, 42.3 MB of additional disk space will be used.\n",
            "Get:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 gettext-base amd64 0.21-4ubuntu4 [37.8 kB]\n",
            "Get:2 http://archive.ubuntu.com/ubuntu jammy/main amd64 autopoint all 0.21-4ubuntu4 [422 kB]\n",
            "Get:3 http://archive.ubuntu.com/ubuntu jammy/main amd64 libdebhelper-perl all 13.6ubuntu1 [67.2 kB]\n",
            "Get:4 http://archive.ubuntu.com/ubuntu jammy/main amd64 libtool all 2.4.6-15build2 [164 kB]\n",
            "Get:5 http://archive.ubuntu.com/ubuntu jammy/main amd64 dh-autoreconf all 20 [16.1 kB]\n",
            "Get:6 http://archive.ubuntu.com/ubuntu jammy/main amd64 libarchive-zip-perl all 1.68-1 [90.2 kB]\n",
            "Get:7 http://archive.ubuntu.com/ubuntu jammy/main amd64 libsub-override-perl all 0.09-2 [9,532 B]\n",
            "Get:8 http://archive.ubuntu.com/ubuntu jammy/main amd64 libfile-stripnondeterminism-perl all 1.13.0-1 [18.1 kB]\n",
            "Get:9 http://archive.ubuntu.com/ubuntu jammy/main amd64 dh-strip-nondeterminism all 1.13.0-1 [5,344 B]\n",
            "Get:10 http://archive.ubuntu.com/ubuntu jammy/main amd64 debugedit amd64 1:5.0-4build1 [47.2 kB]\n",
            "Get:11 http://archive.ubuntu.com/ubuntu jammy/main amd64 dwz amd64 0.14-1build2 [105 kB]\n",
            "Get:12 http://archive.ubuntu.com/ubuntu jammy/main amd64 gettext amd64 0.21-4ubuntu4 [868 kB]\n",
            "Get:13 http://archive.ubuntu.com/ubuntu jammy/main amd64 intltool-debian all 0.35.0+20060710.5 [24.9 kB]\n",
            "Get:14 http://archive.ubuntu.com/ubuntu jammy/main amd64 po-debconf all 1.0.21+nmu1 [233 kB]\n",
            "Get:15 http://archive.ubuntu.com/ubuntu jammy/main amd64 debhelper all 13.6ubuntu1 [923 kB]\n",
            "Get:16 http://archive.ubuntu.com/ubuntu jammy/main amd64 libarchive-cpio-perl all 0.10-1.1 [9,928 B]\n",
            "Get:17 http://archive.ubuntu.com/ubuntu jammy/main amd64 libsys-hostname-long-perl all 1.5-2 [11.5 kB]\n",
            "Get:18 http://archive.ubuntu.com/ubuntu jammy/main amd64 libmail-sendmail-perl all 0.80-1.1 [22.7 kB]\n",
            "Get:19 http://archive.ubuntu.com/ubuntu jammy/universe amd64 libossp-uuid16 amd64 1.6.2-1.5build9 [31.4 kB]\n",
            "Get:20 http://archive.ubuntu.com/ubuntu jammy/universe amd64 swi-prolog-core amd64 8.4.2+dfsg-2ubuntu1 [1,493 kB]\n",
            "Get:21 http://archive.ubuntu.com/ubuntu jammy/universe amd64 swi-prolog-doc all 8.4.2+dfsg-2ubuntu1 [1,504 kB]\n",
            "Get:22 http://archive.ubuntu.com/ubuntu jammy/universe amd64 swi-prolog-core-packages amd64 8.4.2+dfsg-2ubuntu1 [1,335 kB]\n",
            "Get:23 http://archive.ubuntu.com/ubuntu jammy/universe amd64 swi-prolog-nox amd64 8.4.2+dfsg-2ubuntu1 [165 kB]\n",
            "Get:24 http://archive.ubuntu.com/ubuntu jammy/universe amd64 swi-prolog-x amd64 8.4.2+dfsg-2ubuntu1 [2,314 kB]\n",
            "Get:25 http://archive.ubuntu.com/ubuntu jammy/universe amd64 swi-prolog amd64 8.4.2+dfsg-2ubuntu1 [9,122 B]\n",
            "Fetched 9,926 kB in 3s (3,394 kB/s)\n",
            "Selecting previously unselected package gettext-base.\n",
            "(Reading database ... 126284 files and directories currently installed.)\n",
            "Preparing to unpack .../00-gettext-base_0.21-4ubuntu4_amd64.deb ...\n",
            "Unpacking gettext-base (0.21-4ubuntu4) ...\n",
            "Selecting previously unselected package autopoint.\n",
            "Preparing to unpack .../01-autopoint_0.21-4ubuntu4_all.deb ...\n",
            "Unpacking autopoint (0.21-4ubuntu4) ...\n",
            "Selecting previously unselected package libdebhelper-perl.\n",
            "Preparing to unpack .../02-libdebhelper-perl_13.6ubuntu1_all.deb ...\n",
            "Unpacking libdebhelper-perl (13.6ubuntu1) ...\n",
            "Selecting previously unselected package libtool.\n",
            "Preparing to unpack .../03-libtool_2.4.6-15build2_all.deb ...\n",
            "Unpacking libtool (2.4.6-15build2) ...\n",
            "Selecting previously unselected package dh-autoreconf.\n",
            "Preparing to unpack .../04-dh-autoreconf_20_all.deb ...\n",
            "Unpacking dh-autoreconf (20) ...\n",
            "Selecting previously unselected package libarchive-zip-perl.\n",
            "Preparing to unpack .../05-libarchive-zip-perl_1.68-1_all.deb ...\n",
            "Unpacking libarchive-zip-perl (1.68-1) ...\n",
            "Selecting previously unselected package libsub-override-perl.\n",
            "Preparing to unpack .../06-libsub-override-perl_0.09-2_all.deb ...\n",
            "Unpacking libsub-override-perl (0.09-2) ...\n",
            "Selecting previously unselected package libfile-stripnondeterminism-perl.\n",
            "Preparing to unpack .../07-libfile-stripnondeterminism-perl_1.13.0-1_all.deb ...\n",
            "Unpacking libfile-stripnondeterminism-perl (1.13.0-1) ...\n",
            "Selecting previously unselected package dh-strip-nondeterminism.\n",
            "Preparing to unpack .../08-dh-strip-nondeterminism_1.13.0-1_all.deb ...\n",
            "Unpacking dh-strip-nondeterminism (1.13.0-1) ...\n",
            "Selecting previously unselected package debugedit.\n",
            "Preparing to unpack .../09-debugedit_1%3a5.0-4build1_amd64.deb ...\n",
            "Unpacking debugedit (1:5.0-4build1) ...\n",
            "Selecting previously unselected package dwz.\n",
            "Preparing to unpack .../10-dwz_0.14-1build2_amd64.deb ...\n",
            "Unpacking dwz (0.14-1build2) ...\n",
            "Selecting previously unselected package gettext.\n",
            "Preparing to unpack .../11-gettext_0.21-4ubuntu4_amd64.deb ...\n",
            "Unpacking gettext (0.21-4ubuntu4) ...\n",
            "Selecting previously unselected package intltool-debian.\n",
            "Preparing to unpack .../12-intltool-debian_0.35.0+20060710.5_all.deb ...\n",
            "Unpacking intltool-debian (0.35.0+20060710.5) ...\n",
            "Selecting previously unselected package po-debconf.\n",
            "Preparing to unpack .../13-po-debconf_1.0.21+nmu1_all.deb ...\n",
            "Unpacking po-debconf (1.0.21+nmu1) ...\n",
            "Selecting previously unselected package debhelper.\n",
            "Preparing to unpack .../14-debhelper_13.6ubuntu1_all.deb ...\n",
            "Unpacking debhelper (13.6ubuntu1) ...\n",
            "Selecting previously unselected package libarchive-cpio-perl.\n",
            "Preparing to unpack .../15-libarchive-cpio-perl_0.10-1.1_all.deb ...\n",
            "Unpacking libarchive-cpio-perl (0.10-1.1) ...\n",
            "Selecting previously unselected package libsys-hostname-long-perl.\n",
            "Preparing to unpack .../16-libsys-hostname-long-perl_1.5-2_all.deb ...\n",
            "Unpacking libsys-hostname-long-perl (1.5-2) ...\n",
            "Selecting previously unselected package libmail-sendmail-perl.\n",
            "Preparing to unpack .../17-libmail-sendmail-perl_0.80-1.1_all.deb ...\n",
            "Unpacking libmail-sendmail-perl (0.80-1.1) ...\n",
            "Selecting previously unselected package libossp-uuid16:amd64.\n",
            "Preparing to unpack .../18-libossp-uuid16_1.6.2-1.5build9_amd64.deb ...\n",
            "Unpacking libossp-uuid16:amd64 (1.6.2-1.5build9) ...\n",
            "Selecting previously unselected package swi-prolog-core.\n",
            "Preparing to unpack .../19-swi-prolog-core_8.4.2+dfsg-2ubuntu1_amd64.deb ...\n",
            "Unpacking swi-prolog-core (8.4.2+dfsg-2ubuntu1) ...\n",
            "Selecting previously unselected package swi-prolog-doc.\n",
            "Preparing to unpack .../20-swi-prolog-doc_8.4.2+dfsg-2ubuntu1_all.deb ...\n",
            "Unpacking swi-prolog-doc (8.4.2+dfsg-2ubuntu1) ...\n",
            "Selecting previously unselected package swi-prolog-core-packages.\n",
            "Preparing to unpack .../21-swi-prolog-core-packages_8.4.2+dfsg-2ubuntu1_amd64.deb ...\n",
            "Unpacking swi-prolog-core-packages (8.4.2+dfsg-2ubuntu1) ...\n",
            "Selecting previously unselected package swi-prolog-nox.\n",
            "Preparing to unpack .../22-swi-prolog-nox_8.4.2+dfsg-2ubuntu1_amd64.deb ...\n",
            "Unpacking swi-prolog-nox (8.4.2+dfsg-2ubuntu1) ...\n",
            "Selecting previously unselected package swi-prolog-x.\n",
            "Preparing to unpack .../23-swi-prolog-x_8.4.2+dfsg-2ubuntu1_amd64.deb ...\n",
            "Unpacking swi-prolog-x (8.4.2+dfsg-2ubuntu1) ...\n",
            "Selecting previously unselected package swi-prolog.\n",
            "Preparing to unpack .../24-swi-prolog_8.4.2+dfsg-2ubuntu1_amd64.deb ...\n",
            "Unpacking swi-prolog (8.4.2+dfsg-2ubuntu1) ...\n",
            "Setting up libtool (2.4.6-15build2) ...\n",
            "Setting up libarchive-zip-perl (1.68-1) ...\n",
            "Setting up libdebhelper-perl (13.6ubuntu1) ...\n",
            "Setting up gettext-base (0.21-4ubuntu4) ...\n",
            "Setting up libossp-uuid16:amd64 (1.6.2-1.5build9) ...\n",
            "Setting up swi-prolog-core (8.4.2+dfsg-2ubuntu1) ...\n",
            "update-alternatives: using /usr/bin/swipl to provide /usr/bin/prolog (prolog) in auto mode\n",
            "Setting up autopoint (0.21-4ubuntu4) ...\n",
            "Setting up dwz (0.14-1build2) ...\n",
            "Setting up swi-prolog-core-packages (8.4.2+dfsg-2ubuntu1) ...\n",
            "Setting up libarchive-cpio-perl (0.10-1.1) ...\n",
            "Setting up debugedit (1:5.0-4build1) ...\n",
            "Setting up swi-prolog-nox (8.4.2+dfsg-2ubuntu1) ...\n",
            "Setting up libsub-override-perl (0.09-2) ...\n",
            "Setting up libsys-hostname-long-perl (1.5-2) ...\n",
            "Setting up libfile-stripnondeterminism-perl (1.13.0-1) ...\n",
            "Setting up swi-prolog-x (8.4.2+dfsg-2ubuntu1) ...\n",
            "Setting up gettext (0.21-4ubuntu4) ...\n",
            "Setting up swi-prolog-doc (8.4.2+dfsg-2ubuntu1) ...\n",
            "Setting up intltool-debian (0.35.0+20060710.5) ...\n",
            "Setting up dh-autoreconf (20) ...\n",
            "Setting up libmail-sendmail-perl (0.80-1.1) ...\n",
            "Setting up dh-strip-nondeterminism (1.13.0-1) ...\n",
            "Setting up swi-prolog (8.4.2+dfsg-2ubuntu1) ...\n",
            "Setting up po-debconf (1.0.21+nmu1) ...\n",
            "Setting up debhelper (13.6ubuntu1) ...\n",
            "Processing triggers for libc-bin (2.35-0ubuntu3.8) ...\n",
            "/sbin/ldconfig.real: /usr/local/lib/libur_loader.so.0 is not a symbolic link\n",
            "\n",
            "/sbin/ldconfig.real: /usr/local/lib/libtbbbind_2_0.so.3 is not a symbolic link\n",
            "\n",
            "/sbin/ldconfig.real: /usr/local/lib/libtbbmalloc.so.2 is not a symbolic link\n",
            "\n",
            "/sbin/ldconfig.real: /usr/local/lib/libtcm_debug.so.1 is not a symbolic link\n",
            "\n",
            "/sbin/ldconfig.real: /usr/local/lib/libumf.so.0 is not a symbolic link\n",
            "\n",
            "/sbin/ldconfig.real: /usr/local/lib/libhwloc.so.15 is not a symbolic link\n",
            "\n",
            "/sbin/ldconfig.real: /usr/local/lib/libtcm.so.1 is not a symbolic link\n",
            "\n",
            "/sbin/ldconfig.real: /usr/local/lib/libur_adapter_level_zero_v2.so.0 is not a symbolic link\n",
            "\n",
            "/sbin/ldconfig.real: /usr/local/lib/libtbbbind_2_5.so.3 is not a symbolic link\n",
            "\n",
            "/sbin/ldconfig.real: /usr/local/lib/libtbb.so.12 is not a symbolic link\n",
            "\n",
            "/sbin/ldconfig.real: /usr/local/lib/libtbbmalloc_proxy.so.2 is not a symbolic link\n",
            "\n",
            "/sbin/ldconfig.real: /usr/local/lib/libur_adapter_opencl.so.0 is not a symbolic link\n",
            "\n",
            "/sbin/ldconfig.real: /usr/local/lib/libur_adapter_level_zero.so.0 is not a symbolic link\n",
            "\n",
            "/sbin/ldconfig.real: /usr/local/lib/libtbbbind.so.3 is not a symbolic link\n",
            "\n",
            "Processing triggers for man-db (2.10.2-1) ...\n",
            "Collecting pyswip\n",
            "  Downloading pyswip-0.3.3-py3-none-any.whl.metadata (5.1 kB)\n",
            "Collecting langchain-google-genai\n",
            "  Using cached langchain_google_genai-2.1.8-py3-none-any.whl.metadata (7.0 kB)\n",
            "Collecting langgraph\n",
            "  Using cached langgraph-0.5.4-py3-none-any.whl.metadata (6.8 kB)\n",
            "Requirement already satisfied: langchain-core in /usr/local/lib/python3.11/dist-packages (0.3.69)\n",
            "Collecting filetype<2.0.0,>=1.2.0 (from langchain-google-genai)\n",
            "  Downloading filetype-1.2.0-py2.py3-none-any.whl.metadata (6.5 kB)\n",
            "Collecting google-ai-generativelanguage<0.7.0,>=0.6.18 (from langchain-google-genai)\n",
            "  Downloading google_ai_generativelanguage-0.6.18-py3-none-any.whl.metadata (9.8 kB)\n",
            "Requirement already satisfied: pydantic<3,>=2 in /usr/local/lib/python3.11/dist-packages (from langchain-google-genai) (2.11.7)\n",
            "Collecting langgraph-checkpoint<3.0.0,>=2.1.0 (from langgraph)\n",
            "  Downloading langgraph_checkpoint-2.1.1-py3-none-any.whl.metadata (4.2 kB)\n",
            "Collecting langgraph-prebuilt<0.6.0,>=0.5.0 (from langgraph)\n",
            "  Downloading langgraph_prebuilt-0.5.2-py3-none-any.whl.metadata (4.5 kB)\n",
            "Collecting langgraph-sdk<0.2.0,>=0.1.42 (from langgraph)\n",
            "  Downloading langgraph_sdk-0.1.74-py3-none-any.whl.metadata (1.5 kB)\n",
            "Requirement already satisfied: xxhash>=3.5.0 in /usr/local/lib/python3.11/dist-packages (from langgraph) (3.5.0)\n",
            "Requirement already satisfied: langsmith>=0.3.45 in /usr/local/lib/python3.11/dist-packages (from langchain-core) (0.4.7)\n",
            "Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in /usr/local/lib/python3.11/dist-packages (from langchain-core) (8.5.0)\n",
            "Requirement already satisfied: jsonpatch<2.0,>=1.33 in /usr/local/lib/python3.11/dist-packages (from langchain-core) (1.33)\n",
            "Requirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.11/dist-packages (from langchain-core) (6.0.2)\n",
            "Requirement already satisfied: typing-extensions>=4.7 in /usr/local/lib/python3.11/dist-packages (from langchain-core) (4.14.1)\n",
            "Requirement already satisfied: packaging>=23.2 in /usr/local/lib/python3.11/dist-packages (from langchain-core) (25.0)\n",
            "Requirement already satisfied: google-api-core!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0,>=1.34.1 in /usr/local/lib/python3.11/dist-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0,>=1.34.1->google-ai-generativelanguage<0.7.0,>=0.6.18->langchain-google-genai) (2.25.1)\n",
            "Requirement already satisfied: google-auth!=2.24.0,!=2.25.0,<3.0.0,>=2.14.1 in /usr/local/lib/python3.11/dist-packages (from google-ai-generativelanguage<0.7.0,>=0.6.18->langchain-google-genai) (2.38.0)\n",
            "Requirement already satisfied: proto-plus<2.0.0,>=1.22.3 in /usr/local/lib/python3.11/dist-packages (from google-ai-generativelanguage<0.7.0,>=0.6.18->langchain-google-genai) (1.26.1)\n",
            "Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<7.0.0,>=3.20.2 in /usr/local/lib/python3.11/dist-packages (from google-ai-generativelanguage<0.7.0,>=0.6.18->langchain-google-genai) (5.29.5)\n",
            "Requirement already satisfied: jsonpointer>=1.9 in /usr/local/lib/python3.11/dist-packages (from jsonpatch<2.0,>=1.33->langchain-core) (3.0.0)\n",
            "Collecting ormsgpack>=1.10.0 (from langgraph-checkpoint<3.0.0,>=2.1.0->langgraph)\n",
            "  Downloading ormsgpack-1.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (43 kB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m43.7/43.7 kB\u001b[0m \u001b[31m849.3 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hRequirement already satisfied: httpx>=0.25.2 in /usr/local/lib/python3.11/dist-packages (from langgraph-sdk<0.2.0,>=0.1.42->langgraph) (0.28.1)\n",
            "Requirement already satisfied: orjson>=3.10.1 in /usr/local/lib/python3.11/dist-packages (from langgraph-sdk<0.2.0,>=0.1.42->langgraph) (3.11.0)\n",
            "Requirement already satisfied: requests<3,>=2 in /usr/local/lib/python3.11/dist-packages (from langsmith>=0.3.45->langchain-core) (2.32.3)\n",
            "Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in /usr/local/lib/python3.11/dist-packages (from langsmith>=0.3.45->langchain-core) (1.0.0)\n",
            "Requirement already satisfied: zstandard<0.24.0,>=0.23.0 in /usr/local/lib/python3.11/dist-packages (from langsmith>=0.3.45->langchain-core) (0.23.0)\n",
            "Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.11/dist-packages (from pydantic<3,>=2->langchain-google-genai) (0.7.0)\n",
            "Requirement already satisfied: pydantic-core==2.33.2 in /usr/local/lib/python3.11/dist-packages (from pydantic<3,>=2->langchain-google-genai) (2.33.2)\n",
            "Requirement already satisfied: typing-inspection>=0.4.0 in /usr/local/lib/python3.11/dist-packages (from pydantic<3,>=2->langchain-google-genai) (0.4.1)\n",
            "Requirement already satisfied: googleapis-common-protos<2.0.0,>=1.56.2 in /usr/local/lib/python3.11/dist-packages (from google-api-core!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0,>=1.34.1->google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0,>=1.34.1->google-ai-generativelanguage<0.7.0,>=0.6.18->langchain-google-genai) (1.70.0)\n",
            "Requirement already satisfied: grpcio<2.0.0,>=1.33.2 in /usr/local/lib/python3.11/dist-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0,>=1.34.1->google-ai-generativelanguage<0.7.0,>=0.6.18->langchain-google-genai) (1.73.1)\n",
            "Requirement already satisfied: grpcio-status<2.0.0,>=1.33.2 in /usr/local/lib/python3.11/dist-packages (from google-api-core[grpc]!=2.0.*,!=2.1.*,!=2.10.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*,!=2.8.*,!=2.9.*,<3.0.0,>=1.34.1->google-ai-generativelanguage<0.7.0,>=0.6.18->langchain-google-genai) (1.71.2)\n",
            "Requirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/local/lib/python3.11/dist-packages (from google-auth!=2.24.0,!=2.25.0,<3.0.0,>=2.14.1->google-ai-generativelanguage<0.7.0,>=0.6.18->langchain-google-genai) (5.5.2)\n",
            "Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.11/dist-packages (from google-auth!=2.24.0,!=2.25.0,<3.0.0,>=2.14.1->google-ai-generativelanguage<0.7.0,>=0.6.18->langchain-google-genai) (0.4.2)\n",
            "Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.11/dist-packages (from google-auth!=2.24.0,!=2.25.0,<3.0.0,>=2.14.1->google-ai-generativelanguage<0.7.0,>=0.6.18->langchain-google-genai) (4.9.1)\n",
            "Requirement already satisfied: anyio in /usr/local/lib/python3.11/dist-packages (from httpx>=0.25.2->langgraph-sdk<0.2.0,>=0.1.42->langgraph) (4.9.0)\n",
            "Requirement already satisfied: certifi in /usr/local/lib/python3.11/dist-packages (from httpx>=0.25.2->langgraph-sdk<0.2.0,>=0.1.42->langgraph) (2025.7.14)\n",
            "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/dist-packages (from httpx>=0.25.2->langgraph-sdk<0.2.0,>=0.1.42->langgraph) (1.0.9)\n",
            "Requirement already satisfied: idna in /usr/local/lib/python3.11/dist-packages (from httpx>=0.25.2->langgraph-sdk<0.2.0,>=0.1.42->langgraph) (3.10)\n",
            "Requirement already satisfied: h11>=0.16 in /usr/local/lib/python3.11/dist-packages (from httpcore==1.*->httpx>=0.25.2->langgraph-sdk<0.2.0,>=0.1.42->langgraph) (0.16.0)\n",
            "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langsmith>=0.3.45->langchain-core) (3.4.2)\n",
            "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langsmith>=0.3.45->langchain-core) (2.5.0)\n",
            "Requirement already satisfied: pyasn1<0.7.0,>=0.6.1 in /usr/local/lib/python3.11/dist-packages (from pyasn1-modules>=0.2.1->google-auth!=2.24.0,!=2.25.0,<3.0.0,>=2.14.1->google-ai-generativelanguage<0.7.0,>=0.6.18->langchain-google-genai) (0.6.1)\n",
            "Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.11/dist-packages (from anyio->httpx>=0.25.2->langgraph-sdk<0.2.0,>=0.1.42->langgraph) (1.3.1)\n",
            "Downloading pyswip-0.3.3-py3-none-any.whl (36 kB)\n",
            "Downloading langchain_google_genai-2.1.8-py3-none-any.whl (47 kB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m47.8/47.8 kB\u001b[0m \u001b[31m2.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading langgraph-0.5.4-py3-none-any.whl (143 kB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m143.9/143.9 kB\u001b[0m \u001b[31m2.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading filetype-1.2.0-py2.py3-none-any.whl (19 kB)\n",
            "Downloading google_ai_generativelanguage-0.6.18-py3-none-any.whl (1.4 MB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.4/1.4 MB\u001b[0m \u001b[31m15.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading langgraph_checkpoint-2.1.1-py3-none-any.whl (43 kB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m43.9/43.9 kB\u001b[0m \u001b[31m3.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading langgraph_prebuilt-0.5.2-py3-none-any.whl (23 kB)\n",
            "Downloading langgraph_sdk-0.1.74-py3-none-any.whl (50 kB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.3/50.3 kB\u001b[0m \u001b[31m3.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading ormsgpack-1.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (216 kB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m216.5/216.5 kB\u001b[0m \u001b[31m8.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hInstalling collected packages: filetype, pyswip, ormsgpack, langgraph-sdk, langgraph-checkpoint, google-ai-generativelanguage, langgraph-prebuilt, langchain-google-genai, langgraph\n",
            "  Attempting uninstall: google-ai-generativelanguage\n",
            "    Found existing installation: google-ai-generativelanguage 0.6.15\n",
            "    Uninstalling google-ai-generativelanguage-0.6.15:\n",
            "      Successfully uninstalled google-ai-generativelanguage-0.6.15\n",
            "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
            "google-generativeai 0.8.5 requires google-ai-generativelanguage==0.6.15, but you have google-ai-generativelanguage 0.6.18 which is incompatible.\u001b[0m\u001b[31m\n",
            "\u001b[0mSuccessfully installed filetype-1.2.0 google-ai-generativelanguage-0.6.18 langchain-google-genai-2.1.8 langgraph-0.5.4 langgraph-checkpoint-2.1.1 langgraph-prebuilt-0.5.2 langgraph-sdk-0.1.74 ormsgpack-1.10.0 pyswip-0.3.3\n"
          ]
        },
        {
          "output_type": "display_data",
          "data": {
            "application/vnd.colab-display-data+json": {
              "pip_warning": {
                "packages": [
                  "google"
                ]
              },
              "id": "ae60ee2e25174a3bb625822a2e610c36"
            }
          },
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "from pyswip import Prolog\n",
        "from langchain_google_genai import ChatGoogleGenerativeAI\n",
        "from langchain_core.messages import HumanMessage\n",
        "from langchain_core.tools import tool\n",
        "from langgraph.prebuilt import create_react_agent\n",
        "import json\n",
        "\n",
        "GOOGLE_API_KEY = \"Use Your Own API Key Here\"\n",
        "os.environ[\"GOOGLE_API_KEY\"] = GOOGLE_API_KEY\n",
        "\n",
        "llm = ChatGoogleGenerativeAI(model=\"gemini-1.5-flash\", temperature=0)"
      ],
      "metadata": {
        "id": "S3nltxvBt3UA"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "class AdvancedPrologInterface:\n",
        "    def __init__(self):\n",
        "        self.prolog = Prolog()\n",
        "        self._load_knowledge_base()\n",
        "\n",
        "    def _load_knowledge_base(self):\n",
        "        \"\"\"Load comprehensive Prolog knowledge base\"\"\"\n",
        "        rules = [\n",
        "            \"parent(john, mary, alice)\",\n",
        "            \"parent(john, mary, bob)\",\n",
        "            \"parent(bob, susan, charlie)\",\n",
        "            \"parent(alice, david, emma)\",\n",
        "            \"parent(charlie, lisa, frank)\",\n",
        "\n",
        "            \"male(john)\", \"male(bob)\", \"male(david)\", \"male(charlie)\", \"male(frank)\",\n",
        "            \"female(mary)\", \"female(alice)\", \"female(susan)\", \"female(emma)\", \"female(lisa)\",\n",
        "\n",
        "            \"grandparent(X, Z) :- parent(X, _, Y), parent(Y, _, Z)\",\n",
        "            \"sibling(X, Y) :- parent(P1, P2, X), parent(P1, P2, Y), X \\\\= Y\",\n",
        "            \"uncle(X, Y) :- sibling(X, Z), parent(Z, _, Y), male(X)\",\n",
        "            \"aunt(X, Y) :- sibling(X, Z), parent(Z, _, Y), female(X)\",\n",
        "            \"cousin(X, Y) :- parent(P1, _, X), parent(P2, _, Y), sibling(P1, P2)\",\n",
        "\n",
        "            \"factorial(0, 1)\",\n",
        "            \"factorial(N, F) :- N > 0, N1 is N - 1, factorial(N1, F1), F is N * F1\",\n",
        "\n",
        "            \"list_member(X, [X|_])\",\n",
        "            \"list_member(X, [_|T]) :- list_member(X, T)\",\n",
        "            \"list_length([], 0)\",\n",
        "            \"list_length([_|T], N) :- list_length(T, N1), N is N1 + 1\",\n",
        "\n",
        "            \"animal(dog)\", \"animal(cat)\", \"animal(whale)\", \"animal(eagle)\",\n",
        "            \"mammal(dog)\", \"mammal(cat)\", \"mammal(whale)\",\n",
        "            \"bird(eagle)\", \"bird(sparrow)\",\n",
        "            \"can_fly(eagle)\", \"can_fly(sparrow)\",\n",
        "            \"can_swim(whale)\", \"can_swim(fish)\",\n",
        "            \"aquatic_mammal(X) :- mammal(X), can_swim(X)\"\n",
        "        ]\n",
        "\n",
        "        for rule in rules:\n",
        "            try:\n",
        "                self.prolog.assertz(rule)\n",
        "            except Exception as e:\n",
        "                print(f\"Warning: Could not assert rule '{rule}': {e}\")\n",
        "\n",
        "    def query(self, query_string):\n",
        "        \"\"\"Execute Prolog query and return results\"\"\"\n",
        "        try:\n",
        "            results = list(self.prolog.query(query_string))\n",
        "            return results if results else [{\"result\": \"No solutions found\"}]\n",
        "        except Exception as e:\n",
        "            return [{\"error\": f\"Query failed: {str(e)}\"}]"
      ],
      "metadata": {
        "id": "PMD2g0rKt_o2"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "prolog_interface = AdvancedPrologInterface()\n",
        "\n",
        "@tool\n",
        "def family_relationships(query: str) -> str:\n",
        "    \"\"\"\n",
        "    Query family relationships in Prolog format.\n",
        "    Examples: 'parent(john, mary, X)', 'sibling(X, Y)', 'grandparent(X, charlie)'\n",
        "    \"\"\"\n",
        "    results = prolog_interface.query(query)\n",
        "    return json.dumps(results, indent=2)\n",
        "\n",
        "@tool\n",
        "def mathematical_operations(operation: str, number: int) -> str:\n",
        "    \"\"\"\n",
        "    Perform mathematical operations using Prolog.\n",
        "    Supported operations: 'factorial'\n",
        "    Example: operation='factorial', number=5\n",
        "    \"\"\"\n",
        "    if operation == \"factorial\":\n",
        "        query = f\"factorial({number}, Result)\"\n",
        "        results = prolog_interface.query(query)\n",
        "        return json.dumps(results, indent=2)\n",
        "    else:\n",
        "        return json.dumps([{\"error\": f\"Operation '{operation}' not supported\"}])\n",
        "\n",
        "@tool\n",
        "def advanced_queries(query_type: str, entity: str = \"\") -> str:\n",
        "    \"\"\"\n",
        "    Perform advanced relationship queries.\n",
        "    Types: 'all_children', 'all_grandchildren', 'all_siblings', 'all_cousins'\n",
        "    \"\"\"\n",
        "    queries = {\n",
        "        'all_children': f\"parent(_, _, {entity})\" if entity else \"parent(_, _, X)\",\n",
        "        'all_grandchildren': f\"grandparent(_, {entity})\" if entity else \"grandparent(_, X)\",\n",
        "        'all_siblings': f\"sibling({entity}, X)\" if entity else \"sibling(X, Y)\",\n",
        "        'all_cousins': f\"cousin({entity}, X)\" if entity else \"cousin(X, Y)\"\n",
        "    }\n",
        "\n",
        "    if query_type in queries:\n",
        "        results = prolog_interface.query(queries[query_type])\n",
        "        return json.dumps(results, indent=2)\n",
        "    else:\n",
        "        return json.dumps([{\"error\": f\"Query type '{query_type}' not supported\"}])"
      ],
      "metadata": {
        "id": "vL0FjwExufDQ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "tools = [family_relationships, mathematical_operations, advanced_queries]\n",
        "agent = create_react_agent(llm, tools)\n",
        "\n",
        "def run_family_analysis():\n",
        "    \"\"\"Comprehensive family relationship analysis\"\"\"\n",
        "    print(\"👨‍👩‍👧‍👦 Family Relationship Analysis\")\n",
        "    print(\"=\" * 50)\n",
        "\n",
        "    queries = [\n",
        "        \"Who are all the parents in the family database?\",\n",
        "        \"Find all grandparent-grandchild relationships\",\n",
        "        \"Show me all the siblings in the family\",\n",
        "        \"Who are John and Mary's children?\",\n",
        "        \"Calculate the factorial of 6 using Prolog\"\n",
        "    ]\n",
        "\n",
        "    for i, query in enumerate(queries, 1):\n",
        "        print(f\"\\n🔍 Query {i}: {query}\")\n",
        "        print(\"-\" * 30)\n",
        "\n",
        "        try:\n",
        "            response = agent.invoke({\"messages\": [(\"human\", query)]})\n",
        "            answer = response[\"messages\"][-1].content\n",
        "            print(f\"🤖 Response: {answer}\")\n",
        "        except Exception as e:\n",
        "            print(f\"❌ Error: {str(e)}\")\n",
        "\n",
        "def demonstrate_complex_reasoning():\n",
        "    \"\"\"Show advanced multi-step reasoning\"\"\"\n",
        "    print(\"\\n🧠 Complex Multi-Step Reasoning\")\n",
        "    print(\"=\" * 40)\n",
        "\n",
        "    complex_query = \"\"\"\n",
        "    I want a complete family tree analysis. Please:\n",
        "    1. List all parent-child relationships\n",
        "    2. Identify all grandparent relationships\n",
        "    3. Find any uncle/aunt relationships\n",
        "    4. Show cousin relationships\n",
        "    5. Calculate factorial of 4 as a bonus math operation\n",
        "    \"\"\"\n",
        "\n",
        "    print(f\"Complex Query: {complex_query}\")\n",
        "    print(\"-\" * 40)\n",
        "\n",
        "    try:\n",
        "        response = agent.invoke({\"messages\": [(\"human\", complex_query)]})\n",
        "        print(f\"📋 Comprehensive Analysis:\\n{response['messages'][-1].content}\")\n",
        "    except Exception as e:\n",
        "        print(f\"❌ Error in complex reasoning: {str(e)}\")\n",
        "\n",
        "def interactive_prolog_session():\n",
        "    \"\"\"Interactive Prolog knowledge base exploration\"\"\"\n",
        "    print(\"\\n💬 Interactive Prolog Explorer\")\n",
        "    print(\"Ask about family relationships, math operations, or general queries!\")\n",
        "    print(\"Type 'examples' to see sample queries, 'quit' to exit\")\n",
        "    print(\"-\" * 50)\n",
        "\n",
        "    examples = [\n",
        "        \"Who are Bob's children?\",\n",
        "        \"Find all grandparents in the family\",\n",
        "        \"Calculate factorial of 5\",\n",
        "        \"Show me all cousin relationships\",\n",
        "        \"Who are Alice's siblings?\"\n",
        "    ]\n",
        "\n",
        "    while True:\n",
        "        user_input = input(\"\\n🧑 You: \")\n",
        "\n",
        "        if user_input.lower() == 'quit':\n",
        "            print(\"👋 Goodbye!\")\n",
        "            break\n",
        "        elif user_input.lower() == 'examples':\n",
        "            print(\"📝 Example queries:\")\n",
        "            for ex in examples:\n",
        "                print(f\"  • {ex}\")\n",
        "            continue\n",
        "\n",
        "        try:\n",
        "            response = agent.invoke({\"messages\": [(\"human\", user_input)]})\n",
        "            print(f\"🤖 AI: {response['messages'][-1].content}\")\n",
        "        except Exception as e:\n",
        "            print(f\"❌ Error: {str(e)}\")"
      ],
      "metadata": {
        "id": "Y6nVn6UTuk9F"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Hezwj6SDsIGz",
        "outputId": "9ac03ea5-e116-41e5-ea33-e29cb70221df"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "🚀 Advanced Prolog + Gemini Integration\n",
            "Using PySwip for stable Prolog integration\n",
            "=======================================================\n",
            "\n",
            "🔬 Direct Prolog Query Testing\n",
            "===================================\n",
            "\n",
            "📋 Find John and Mary's children\n",
            "Query: parent(john, mary, X)\n",
            "Results: [\n",
            "  {\n",
            "    \"X\": \"alice\"\n",
            "  },\n",
            "  {\n",
            "    \"X\": \"bob\"\n",
            "  }\n",
            "]\n",
            "\n",
            "📋 Find Charlie's grandparents\n",
            "Query: grandparent(X, charlie)\n",
            "Results: [\n",
            "  {\n",
            "    \"X\": \"john\"\n",
            "  }\n",
            "]\n",
            "\n",
            "📋 Find Alice's siblings\n",
            "Query: sibling(alice, X)\n",
            "Results: [\n",
            "  {\n",
            "    \"X\": \"bob\"\n",
            "  }\n",
            "]\n",
            "\n",
            "📋 Calculate 4 factorial\n",
            "Query: factorial(4, X)\n",
            "Results: [\n",
            "  {\n",
            "    \"X\": 24\n",
            "  }\n",
            "]\n",
            "\n",
            "📋 Find all cousin pairs\n",
            "Query: cousin(X, Y)\n",
            "Results: [\n",
            "  {\n",
            "    \"X\": \"charlie\",\n",
            "    \"Y\": \"emma\"\n",
            "  },\n",
            "  {\n",
            "    \"X\": \"emma\",\n",
            "    \"Y\": \"charlie\"\n",
            "  }\n",
            "]\n",
            "👨‍👩‍👧‍👦 Family Relationship Analysis\n",
            "==================================================\n",
            "\n",
            "🔍 Query 1: Who are all the parents in the family database?\n",
            "------------------------------\n",
            "🤖 Response: The available tools don't directly list all parents.  To get that information, I need to query the database for all individuals who have children.  However, the `family_relationships` function requires a specific query in Prolog format, and I don't know the structure of your family data to construct such a query.  I need more information about how the family data is represented.\n",
            "\n",
            "🔍 Query 2: Find all grandparent-grandchild relationships\n",
            "------------------------------\n",
            "🤖 Response: The query returned the following grandchild-grandparent pairs: emma, charlie and frank.  Note that the specific grandparent for each is not identified in this response.  To get more detail, a different query would be needed.\n",
            "\n",
            "🔍 Query 3: Show me all the siblings in the family\n",
            "------------------------------\n",
            "🤖 Response: Alice and Bob are siblings.\n",
            "\n",
            "🔍 Query 4: Who are John and Mary's children?\n",
            "------------------------------\n",
            "🤖 Response: John and Mary's children are alice and bob.\n",
            "\n",
            "🔍 Query 5: Calculate the factorial of 6 using Prolog\n",
            "------------------------------\n",
            "🤖 Response: The factorial of 6 is 720.\n",
            "\n",
            "🧠 Complex Multi-Step Reasoning\n",
            "========================================\n",
            "Complex Query: \n",
            "    I want a complete family tree analysis. Please:\n",
            "    1. List all parent-child relationships\n",
            "    2. Identify all grandparent relationships  \n",
            "    3. Find any uncle/aunt relationships\n",
            "    4. Show cousin relationships\n",
            "    5. Calculate factorial of 4 as a bonus math operation\n",
            "    \n",
            "----------------------------------------\n",
            "📋 Comprehensive Analysis:\n",
            "The available tools are not able to generate a complete family tree analysis.  The `family_relationships` and `advanced_queries` functions are failing, likely due to issues with the underlying Prolog implementation or the format of the queries.  The factorial calculation worked correctly, returning 24.  To get a complete family tree analysis, more robust family relationship querying capabilities are needed.\n",
            "\n",
            "🔢 Mathematical Reasoning with Prolog\n",
            "========================================\n",
            "\n",
            "🧮 Math Query: Calculate factorial of 3, 4, and 5\n",
            "📊 Result: The factorial of 3 is 6.  I wasn't able to calculate the factorials of 4 and 5. There may be a bug in the available tools.\n",
            "\n",
            "🧮 Math Query: What is the factorial of 7?\n",
            "📊 Result: The factorial of 7 is 5040.\n",
            "\n",
            "🧮 Math Query: Show me how factorial calculation works step by step\n",
            "📊 Result: I cannot directly show the step-by-step calculation of a factorial using the provided `default_api`.  The `mathematical_operations` function only provides the final result, not the intermediate steps.  To see the steps, you would need a different function or library that explicitly details the factorial calculation process.\n",
            "\n",
            "✅ Tutorial completed successfully!\n",
            "🎯 Key achievements:\n",
            "  • Integrated PySwip with Gemini AI\n",
            "  • Created advanced Prolog reasoning tools\n",
            "  • Demonstrated complex family relationship queries\n",
            "  • Implemented mathematical operations in Prolog\n",
            "  • Built interactive AI agent with logical reasoning\n",
            "\n",
            "🚀 Try extending with your own Prolog rules and facts!\n"
          ]
        }
      ],
      "source": [
        "def test_direct_queries():\n",
        "    \"\"\"Test direct Prolog queries for verification\"\"\"\n",
        "    print(\"\\n🔬 Direct Prolog Query Testing\")\n",
        "    print(\"=\" * 35)\n",
        "\n",
        "    test_queries = [\n",
        "        (\"parent(john, mary, X)\", \"Find John and Mary's children\"),\n",
        "        (\"grandparent(X, charlie)\", \"Find Charlie's grandparents\"),\n",
        "        (\"sibling(alice, X)\", \"Find Alice's siblings\"),\n",
        "        (\"factorial(4, X)\", \"Calculate 4 factorial\"),\n",
        "        (\"cousin(X, Y)\", \"Find all cousin pairs\")\n",
        "    ]\n",
        "\n",
        "    for query, description in test_queries:\n",
        "        print(f\"\\n📋 {description}\")\n",
        "        print(f\"Query: {query}\")\n",
        "        results = prolog_interface.query(query)\n",
        "        print(f\"Results: {json.dumps(results, indent=2)}\")\n",
        "\n",
        "\n",
        "def main():\n",
        "    \"\"\"Main demonstration runner\"\"\"\n",
        "    if GOOGLE_API_KEY == \"YOUR_GEMINI_API_KEY_HERE\":\n",
        "        print(\"⚠️  Please set your Gemini API key in Cell 3!\")\n",
        "        print(\"Get it from: https://aistudio.google.com/app/apikey\")\n",
        "        return\n",
        "\n",
        "    print(\"🚀 Advanced Prolog + Gemini Integration\")\n",
        "    print(\"Using PySwip for stable Prolog integration\")\n",
        "    print(\"=\" * 55)\n",
        "\n",
        "    test_direct_queries()\n",
        "    run_family_analysis()\n",
        "    demonstrate_complex_reasoning()\n",
        "\n",
        "\n",
        "def show_mathematical_capabilities():\n",
        "    \"\"\"Demonstrate mathematical reasoning with Prolog\"\"\"\n",
        "    print(\"\\n🔢 Mathematical Reasoning with Prolog\")\n",
        "    print(\"=\" * 40)\n",
        "\n",
        "    math_queries = [\n",
        "        \"Calculate factorial of 3, 4, and 5\",\n",
        "        \"What is the factorial of 7?\",\n",
        "        \"Show me how factorial calculation works step by step\"\n",
        "    ]\n",
        "\n",
        "    for query in math_queries:\n",
        "        print(f\"\\n🧮 Math Query: {query}\")\n",
        "        try:\n",
        "            response = agent.invoke({\"messages\": [(\"human\", query)]})\n",
        "            print(f\"📊 Result: {response['messages'][-1].content}\")\n",
        "        except Exception as e:\n",
        "            print(f\"❌ Error: {str(e)}\")\n",
        "\n",
        "if __name__ == \"__main__\":\n",
        "    main()\n",
        "    show_mathematical_capabilities()\n",
        "\n",
        "    print(\"\\n✅ Tutorial completed successfully!\")\n",
        "    print(\"🎯 Key achievements:\")\n",
        "    print(\"  • Integrated PySwip with Gemini AI\")\n",
        "    print(\"  • Created advanced Prolog reasoning tools\")\n",
        "    print(\"  • Demonstrated complex family relationship queries\")\n",
        "    print(\"  • Implemented mathematical operations in Prolog\")\n",
        "    print(\"  • Built interactive AI agent with logical reasoning\")\n",
        "    print(\"\\n🚀 Try extending with your own Prolog rules and facts!\")"
      ]
    }
  ]
}